const DEFAULTS = {
  engine: "bing",
  windowWidth: 500,
  windowHeight: 550,
  openInPopup: true,
  modifierKey: "Alt", // 新增：默认修饰键
  triggerKey: "LeftClick" // 新增：默认触发键
};

function $(id) { return document.getElementById(id); }

function restore() {
  chrome.storage.sync.get(DEFAULTS, (cfg) => {
    $("engine").value = cfg.engine;
    $("width").value = cfg.windowWidth;
    $("height").value = cfg.windowHeight;
    $("openInPopup").checked = !!cfg.openInPopup;
    $("modifierKey").value = cfg.modifierKey;
    $("triggerKey").value = cfg.triggerKey;
  });
}

function save() {
  $("save").disabled = true;
  const cfg = {
    engine: $("engine").value,
    windowWidth: Math.max(320, Math.min(1200, parseInt($("width").value || 480, 10))),
    windowHeight: Math.max(240, Math.min(1000, parseInt($("height").value || 360, 10))),
    openInPopup: $("openInPopup").checked,
    modifierKey: $("modifierKey").value,
    triggerKey: $("triggerKey").value
  };
  chrome.storage.sync.set(cfg, () => {
    $("status").textContent = "✅ 已保存";
    $("save").disabled = false;
    setTimeout(() => ($("status").textContent = ""), 1500);
  });
}

restore();
$("save").addEventListener("click", save);